<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/9/8
 * Time: 09:28
 */

namespace Common\Utils;


use Think\Log;

class TemplateUtils
{
    private $utils;
    function __construct()
    {
        $APPID = C('APPID');
        $APPSECRET = C('APPSECRET');
        $TOKEN = C('TOKEN');
        $this->utils =  new WeChatUtils($APPID, $APPSECRET, $TOKEN);
    }

    /**
     * 发送判定提醒消息
     * @param $openid
     * @param $room_id int 房间id
     */
    public function sendJudgeMessage($openid,$room_id){
        $data['touser'] = $openid;
        $data['template_id'] = Constants::TEMPLATE_JUDGE_ID;
        $data['data']['first']['value'] = "您有一个比赛(擂台号:$room_id)等待判定，请登录后台判定结果！";
        //发起人
        $data['data']['keyword1']['value'] = "擂台号:$room_id";
        $data['data']['keyword2']['value'] = '登录后台判定结果！';
        $data['data']['remark']['value'] = '请尽快去平台后台查看详情';
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendJudgeMessage errcode---:'.$result->errcode);
        Log::write('sendVerifyMessage errmsg---:'.$result->errmsg);
    }

    /**
     * 发送匹配成功消息
     * @param $openid
     * @param $room_id
     */
    public function sendMatchMessage($openid,$room_id){
        $data['touser'] = $openid;
        Log::write('sendVerifyMessage touser---:'.$openid);
        $data['template_id'] = Constants::TEMPLATE_MATCH_ID;
        $data['data']['first']['value'] = "您好，您的擂台(擂台号：$room_id)已经匹配到对手！";
        //匹配时间
        $data['data']['keyword1']['value'] = date('Y年m月d日 H:i',time());
        $data['data']['remark']['value'] = '请尽快去擂台查看详情';
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendMatchMessage errcode---:'.$result->errcode);
        Log::write('sendVerifyMessage errmsg---:'.$result->errmsg);
    }

    /**
     * 比赛结果通知
     * @param $openid
     * @param $nickname
     * @param $room_id
     * @param $type 0失败 1成功
     */
    public function sendVerifyMessage($openid,$nickname,$room_id,$type){
        $data['touser'] = $openid;
        $data['template_id'] = Constants::TEMPLATE_VERIFY_ID;
        if($type == 1){
            $str = '胜利';
        }else{
            $str = '失败';
        }
        $data['data']['first']['value'] = "您好，您的比赛(擂台号：$room_id)已经审核完毕，您获得".$str;
        //匹配时间
        $data['data']['keyword3']['value'] = date('Y年m月d日 H:i',time());
        $data['data']['keyword1']['value'] = $nickname;
        $data['data']['keyword2']['value'] = $str;
        $data['data']['remark']['value'] = '请尽快去个人中心查看详情';
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendVerifyMessage errcode---:'.$result->errcode);
        Log::write('sendVerifyMessage errmsg---:'.$result->errmsg);
        Log::write('sendVerifyMessage openid---:'.$openid);
    }

    /**
     * 发送邀请通知
     * @param $openid
     * @param $nickname
     * @param $room_id
     */
    public function sendInviteMessage($openid,$nickname,$room_id){
        $data['touser'] = $openid;
        $data['template_id'] = Constants::TEMPLATE_INVITE;
        $data['data']['first']['value'] = "您好，$nickname 邀请你打擂(擂台号：$room_id)";
        //匹配时间
        $data['data']['keyword2']['value'] = date('Y年m月d日 H:i',time());
        $data['data']['keyword1']['value'] = '打擂邀请';
        $data['data']['remark']['value'] = '请尽快去个人中心查看详情';
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendInviteMessage errcode---:'.$result->errcode);
        Log::write('sendInviteMessage errmsg---:'.$result->errmsg);
        Log::write('sendInviteMessage openid---:'.$openid);
    }

    /**
     * 解散申请通知
     * @param $openid
     * @param $nickname
     * @param $room_id
     */
    public function sendDisbandedWaitMessage($openid,$nickname,$room_id){
        $data['touser'] = $openid;
        $data['template_id'] = Constants::TEMPLATE_DISBANDED_WAIT;
        $data['data']['first']['value'] = "您好，$nickname 请求解散擂台(擂台号：$room_id)";
        //匹配时间
        $data['data']['keyword2']['value'] = date('Y年m月d日 H:i',time());
        $data['data']['keyword1']['value'] = "擂台号：$room_id";
        $data['data']['remark']['value'] = '请尽快去平台后台查看详情';
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendInviteMessage errcode---:'.$result->errcode);
        Log::write('sendInviteMessage errmsg---:'.$result->errmsg);
        Log::write('sendInviteMessage openid---:'.$openid);
    }

    /**
     * 解散成功通知
     * @param $openid
     * @param $remark
     * @param $room_id
     * @param $type
     */
    public function sendDisbandedSuccessMessage($openid,$remark,$room_id,$type){
        $data['touser'] = $openid;
        $data['template_id'] = Constants::TEMPLATE_DISBANDED_SUCCESS;
        if($type == 1){
            $str = '已经审核成功';
            $sys_remark = '打擂费用已经退回，请到个人中心查看详情！';
        }else{
            $str = '审核失败';
            $sys_remark = '失败原因：'.$remark;
        }
        $data['data']['first']['value'] = "您好，您请求解散擂台(擂台号：$room_id)，".$str;
        //匹配时间
        $data['data']['keyword2']['value'] = date('Y年m月d日 H:i',time());
        $data['data']['keyword1']['value'] = "擂台号：$room_id";
        $data['data']['remark']['value'] = $sys_remark;
        $access_token = $this->utils->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data = json_encode($data);//将数据转化为json
        $result = $this->utils->requestHttp($url,$data);
        $result = json_decode($result);
        Log::write('sendInviteMessage errcode---:'.$result->errcode);
        Log::write('sendInviteMessage errmsg---:'.$result->errmsg);
        Log::write('sendInviteMessage openid---:'.$openid);
    }

}